﻿using System;
using System.Windows.Forms;
using xxpl1.Log;

namespace ProxyUI
{
    public class FormAppender : AbstractLogger
    {
        public delegate void LogDelegate(string log);
        private TextBox loggintTextBox;
        private bool log;

        public void AppendTextBox(TextBox textBox)
        {
            loggintTextBox = textBox;
        }

        public void TurnOffLog(bool log)
        {
            this.log = log;
        }

        private void OutputlogToTextBox(string log)
        {
            if (loggintTextBox.TextLength > 80960)
            {
                loggintTextBox.Clear();
            }
            loggintTextBox.Paste(log);
            loggintTextBox.Paste(Environment.NewLine);
        }

        protected override void Append(string message)
        {
            if (loggintTextBox == null || !log) return;

            loggintTextBox.Invoke(new LogDelegate(OutputlogToTextBox), message);
        }
    }
}
